# -*- coding: UTF-8 -*-
import time

from selenium import webdriver
import selenium.webdriver.support.ui as ui
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By


username = "你的用户名"
password = "你们的密码"

if __name__ == '__main__':
    try:
        # 这里！！！！实现不关闭的重点
        option = webdriver.ChromeOptions()
        option.add_experimental_option("detach", True)
        driver = webdriver.Chrome(options=option)
        driver.get("http://seat.axhu.edu.cn/self")
        #输入账号
        driver.find_elements_by_class_name("input1")[0].send_keys(username)
        #输入密码
        driver.find_elements_by_class_name("input2")[0].send_keys(password)
        #点击验证码
        driver.find_elements_by_class_name("verifyCode")[0].click()
        #等待验证码框消失
        ui.WebDriverWait(driver, 60).until_not(EC.presence_of_element_located((By.ID,"layui-layer1")),message="")
        #点击登录
        driver.find_elements_by_class_name("btn1")[0].click()
        #time.sleep(1)
    except Exception as e:
        print(e)

    while True:
        try:
            #点击查询按钮
            driver.find_elements_by_id("searchBtn")[0].click()
            time.sleep(1)
        except Exception as e:
            print(e)


        for i in range(1, 20):
            try:
                    try:
                        while True:
                            driver.find_element_by_class_name('layui-layer layer-anim layui-layer-iframe')
                            time.sleep(1)
                    except Exception as e:
                        #能到这里来就是没有验证码
                        #点击向下翻页
                        driver.find_element_by_xpath("/html/body/div[4]/div[2]/div/div[2]/div[2]/div/a[2]").click()
                        # 等待加载框消失
                        ui.WebDriverWait(driver, 5).until_not(EC.visibility_of_element_located((By.CLASS_NAME, "dialog")),message="")

                        try:
                            xpath_list = driver.find_elements_by_xpath('//*[@id="seats"]/ul/li[@class="free"]')
                            for list in xpath_list:
                                    print("-------------------\r\n")
                                    print(list.get_attribute('textContent'))
                                    list.click()
                            xpath_list = driver.find_elements_by_xpath('//*[@id="seats"]/ul/li[@class="order"]')
                            for list in xpath_list:
                                print("-------------------\r\n")
                                print(list.get_attribute('textContent'))
                                list.click()
                        except Exception as e:
                            print(e)

            except Exception as e:
                print(e)


